version development
#version 1.0


workflow ClinPatho_tNGS {
    input {
        String project
        Array[String] samples
        Array[File] fastqs
        Directory human_genome
        Directory pathogen_genome
        Directory plasmid_genome
        Directory? nt_genome
        File pathogen_summary_tsv
        File nt_summary_tsv
        Array[String] ctrl_samples
        
    }
    scatter (i in range(length(samples))) {
        call Fastp {
            input:
            sample = samples[i],
            fastq = fastqs[i],
        }
        call RemoveInterference as RemoveHuman {
            input:
            fastq = Fastp.cleaned_fastq,
            genome = human_genome,
            prefix = samples[i] + ".human",
        }
        call RemoveInterference as RemovePlasmid {
            input:
            fastq = RemoveHuman.unmap_fastq,
            genome = plasmid_genome,
            prefix = samples[i] + ".human.plasmid",
        }
        scatter (j in range(3)) {
            call AlignPathogen {
                input:
                fastx = RemovePlasmid.unmap_fastq,
                genome = pathogen_genome,
                prefix = samples[i] + ".pathogen",
                cpu = 32,
                memory = "64G",
                build = j + 1
            }
        }
        call Pathogen {
            input:
            fastp_json = Fastp.fastp_json,
            pathogen_sam = AlignPathogen.sam,
            pathogen_summary_tsv = pathogen_summary_tsv,
            sample = samples[i]
        }
        if (defined(nt_genome)) {
            scatter (j in range(25)) {
                call AlignPathogen as AlignNT {
                    input:
                    fastx = Pathogen.fasta,
                    genome = select_first([nt_genome]),
                    prefix = samples[i] + ".nt",
                    cpu = 32,
                    memory = "128G",
                    build= j + 1
                }
            }
            call NTCheck {
                input:
                nt_sam = AlignNT.sam,
                nt_summary_tsv = nt_summary_tsv,
                pathogen_reads_tsv = Pathogen.reads_tsv,
                sample = samples[i]
            }
        }
        call ResFinder {
            input:
            sample = samples[i],
            human_unmap_fastq = RemoveHuman.unmap_fastq,
        }
        call VirulenceFinder {
            input:
            sample = samples[i],
            human_unmap_fastq = RemoveHuman.unmap_fastq,
        }
    }
    call Result {
        input:
        pathogen_tsv = Pathogen.result_tsv,
        resfinder_tsv = ResFinder.result_tsv,
        virulencefinder_tsv = VirulenceFinder.result_tsv,
        nt_summary_tsv = nt_summary_tsv,
        ctrl_samples = ctrl_samples,
        prefix = project,
        nt_check_tsv = NTCheck.check_tsv,
    }
}

# 质控过滤
task Fastp {
    input {
        String docker = "registry.cn-shanghai.aliyuncs.com/kszy-biosoft/fastp:v0.23.1"
        Int cpu = 32
        String memory = "64G"
        String sample
        File fastq
    }
    command {
        set -vex
        fastp --reads_to_process 36000000 -u 10 -e 30 -w ${cpu} -y -Y 60 -l 35 \
            -i ${fastq} -o ${sample}.cleaned.fastq.gz -j ${sample}.fastp.json -h ${sample}.fastp.html
    }
    output {
        File cleaned_fastq = "${sample}.cleaned.fastq.gz"
        File fastp_json = "${sample}.fastp.json"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}


# 排除干扰数据 人源、质粒等
task RemoveInterference {
    input {
        String docker = "registry.cn-shanghai.aliyuncs.com/kszy-biosoft/bbtools:v39.13"
        Int cpu = 32
        String memory = "64G"
        File fastq
        Directory genome
        String prefix
    }
    command {
        set -vex

        bbmap.sh -Xmx${memory} \
            in=${fastq} \
            outu=${prefix}.unmap.fastq.gz \
            statsfile=${prefix}.stat.txt \
            path=${genome} \
            fast=t \
            threads=${cpu}

    }
    output {
        File unmap_fastq = "${prefix}.unmap.fastq.gz"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}

# 比对pathogen/pathoNT数据库
task AlignPathogen {
    input {
        String docker = "registry.cn-shanghai.aliyuncs.com/kszy-biosoft/bbtools:v39.13"
        Int cpu = 32
        String memory = "64G"
        File fastx
        Directory genome
        String prefix
        Int build
        Float idfilter = 0.9
        Int indelfilter=3
    }
    command {
        set -vex 
        bbmap.sh -Xmx${memory} \
            in=${fastx} \
            outm=${prefix}.${build}.sam \
            path=${genome} \
            build=${build} \
            idfilter=${idfilter} \
            indelfilter=${indelfilter} \
            ambiguous=all \
            fast=t \
            noheader=t  
    }
    output {
        File sam = "${prefix}.${build}.sam"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}


# Pathogen分类
task Pathogen {
    input {
        String docker = "clinpatho:latest"
        Int cpu = 24
        String memory = "192G"
        File fastp_json
        Array[File] pathogen_sam
        File pathogen_summary_tsv
        String sample
    }
    command {
        set -vex
        clinpatho patho  \
            -sam ${sep=" " pathogen_sam} \
            -fastp ${fastp_json} \
            -summary ${pathogen_summary_tsv} \
            -out ${sample}
    }
    output {
        File result_tsv = "${sample}.pathogen.tsv"
        File reads_tsv = "${sample}.reads.tsv"
        File fasta = "${sample}.reads.fasta"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}

#比对NT数据库
task NTCheck {
    input {
        String docker = "clinpatho:latest"
        Int cpu = 24
        String memory = "192G"
        Array[File] nt_sam
        File nt_summary_tsv
        File pathogen_reads_tsv
        String sample
    }
    command {
        set -vex
        clinpatho nt \
            -sam ${sep=" " nt_sam} \
            -reads ${pathogen_reads_tsv} \
            -summary ${nt_summary_tsv} \
            -check ${sample}.nt_check.tsv
    }
    output {
        File check_tsv = "${sample}.nt_check.tsv"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}
# 耐药基因检测
task ResFinder {
    input {
        String docker = "registry.cn-shanghai.aliyuncs.com/kszy-biosoft/resfinder:v4.6.0"
        Int cpu = 4
        String memory = "8G"
        File human_unmap_fastq
        String sample
    }
    command {
        set -vex
        python -m resfinder -ifq ${human_unmap_fastq} -o . -acq -s "Other" -l 0.6 -t 0.8 && \
        cp ResFinder_results_tab.txt ${sample}.res.tsv
    }
    output {
        File result_tsv = "${sample}.res.tsv"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}

# 毒力因子检测
task VirulenceFinder {
    input {
        String docker = "registry.cn-shanghai.aliyuncs.com/kszy-biosoft/virulencefinder:v3.1.0"
        Int cpu = 4
        String memory = "8G"
        File human_unmap_fastq
        String sample
    }
    command {
        set -vex
        python -m virulencefinder -ifq ${human_unmap_fastq} -o . -l 0.6 -t 0.8 -x && \
        cp results_tab.tsv ${sample}.vf.tsv
    }
    output {
        File result_tsv = "${sample}.vf.tsv"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}


# 结果
task Result {
    input {
        String docker = "clinpatho:latest"
        Int cpu = 4
        String memory = "8G"
        Array[File] pathogen_tsv
        Array[File] resfinder_tsv
        Array[File] virulencefinder_tsv
        Array[String] ctrl_samples
        File nt_summary_tsv
        String prefix
        Array[File?] nt_check_tsv
    }
    Array[File] valid_nt_check_tsv = select_all(nt_check_tsv)
    command {
        set -vex
        clinpatho result \
            -patho ${sep=" " pathogen_tsv} \
            -res ${sep=" " resfinder_tsv} \
            -vf ${sep=" " virulencefinder_tsv} \
            -summary ${nt_summary_tsv} \
            -out ${prefix}.result.zip \
            -ctrl ${sep=" " ctrl_samples} \
            ${true='-nt' false='' length(valid_nt_check_tsv) > 0} ${sep=" " valid_nt_check_tsv}
    }
    output {
        File result_zip = "${prefix}.result.zip"
    }
    runtime {
        docker: docker
        memory: memory
        cpu: cpu
    }
}



